#pragma once


#include "IDecoder.h"
#include "Utils.h"
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#ifndef VIDEODECODER_H_
#define VIDEODECODER_H_

#define BYTE char

//#define VIDEODECODER_API extern "C" __declspec(dllexport)

//#ifndef _WINGDI_
//#define _WINGDI_
//typedef struct tagBITMAPFILEHEADER {
//	WORD    bfType;
//	DWORD   bfSize;
//	WORD    bfReserved1;
//	WORD    bfReserved2;
//	DWORD   bfOffBits;
//} BITMAPFILEHEADER, FAR *LPBITMAPFILEHEADER, *PBITMAPFILEHEADER;
//
//typedef struct tagBITMAPINFOHEADER{
//	DWORD      biSize;
//	LONG       biWidth;
//	LONG       biHeight;
//	WORD       biPlanes;
//	WORD       biBitCount;
//	DWORD      biCompression;
//	DWORD      biSizeImage;
//	LONG       biXPelsPerMeter;
//	LONG       biYPelsPerMeter;
//	DWORD      biClrUsed;
//	DWORD      biClrImportant;
//} BITMAPINFOHEADER, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;
//#endif

typedef struct tagPSPacketBuffer{
	unsigned char* packetBuffer;
	int bufferSize;
} PSPacketBuffer;

typedef struct tagESPacketBuffer{
	unsigned char* packetBuffer;
	int bufferSize;
} ESPacketBuffer;

typedef struct tagPacketBuffer{
	unsigned char* packetBuffer;
	int bufferSize;
} PacketBuffer;

//typedef void (*MapMemoryCB)(void** opaque, int width, int height, void* pUser); 

typedef struct tagDecoderHandle{
	void* decoder;
	int decode_mode;
} DecoderHandle;

enum decode_mode
{
	PLAY_PS_FILE,
	PLAY_PS_STREAM,
	SAVE_PS_STREAM,
	PLAY_DH_FILE,
	PLAY_DH_STREAM,
	PLAY_DH_RTSP_STREAM,
	PLAY_RTSP_STREAM,
	PLAY_NORMAL_FILE,
	PLAY_HK_FILE,
	PLAY_CAMYU_STREAM,
	PLAY_ES_STREAM
};

enum decode_state
{
	PLAY,
	PAUSE,
	STOP
};

#define MAX_FRAME_SIZE 1024 * 1024
#define MAX_PAKCET_SIZE 1024 * 10
#define QUEUE_SIZE 300
#define LFDS_SUCCESS 1
#define NORMAL_WAIT_TIME 2

using namespace video_decoder;

class VideoDecoder
{
public:
	VideoDecoder(void);
	virtual ~VideoDecoder(void);

	DecodeCB DecodeCBFun;
	char* pRGBData;
	FRAME_INFORMATION frameInfo;
	virtual void startRecvThread() = 0;
	virtual void startDecodeThread() = 0;
	//bool startRunning;
	bool decodeThreadRunning;
	bool recvThreadRunning;
	pthread_t captureThread;
	pthread_t decodeThread;
	pthread_t recvThread; // for Camyu camera

	int progress;
	int operationFlag;
	int playFlag;
	int width;
	int height;
	void* pUser;
	char* description;

	SwsContext* pSwsCtxBGR32;
	AVFrame* frame;
	AVFrame* frameBGR;

	struct lfds611_queue_state *qs_pool;
	struct lfds611_queue_state *qs_packet;

	int frameCount;
	bool initSocket();
	int port;

	bool saveBMP(char* filename, unsigned char* RGBData, int bpp);

protected:
	int RecvSocket;
    sockaddr_in RecvAddr;
    sockaddr_in SenderAddr;
	int SenderAddrSize;

};





#endif
